//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FixedDividend.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Cashflows/Dividend.h>
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (Double amount, DateTime date) : CDividend(CFixedDividend::typeid)
{
    try
    {
#ifdef HANDLE
        _phFixedDividend = NULL;
#endif
        QuantLib::Real _amount = (QuantLib::Real)ValueHelper::Convert (amount); //d
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //d
        _ppFixedDividend = new boost::shared_ptr<QuantLib::FixedDividend> (new QuantLib::FixedDividend ( _amount,  _date ));
        SetDividend (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFixedDividend));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (boost::shared_ptr<QuantLib::FixedDividend>& childNative, Object^ owner) : CDividend(CFixedDividend::typeid)
{
#ifdef HANDLE
	_phFixedDividend = NULL;
#endif
	_ppFixedDividend = &childNative;
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFixedDividend));
}
Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (QuantLib::FixedDividend& childNative, Object^ owner) : CDividend(CFixedDividend::typeid)
{
#ifdef HANDLE
	_phFixedDividend = NULL;
#endif
	_ppFixedDividend = new boost::shared_ptr<QuantLib::FixedDividend> (&childNative);
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFixedDividend));
    _FixedDividendOwner = owner;
    _DividendOwner = owner;
}

Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (CFixedDividend^ copy) : CDividend(CFixedDividend::typeid)
{
#ifdef HANDLE
	_phFixedDividend = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFixedDividend = new boost::shared_ptr<QuantLib::FixedDividend> (copy->GetShared());
        _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFixedDividend));
    }
}
Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (PLATFORM::Type^ t) : CDividend(CFixedDividend::typeid)
{
#ifdef HANDLE
	_phFixedDividend = NULL;
#endif
	if (!t->IsSubclassOf(CFixedDividend::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (QuantLib::Handle<QuantLib::FixedDividend>& childNative, Object^ owner)  : CDividend(CFixedDividend::typeid)
{
	_phFixedDividend = &childNative;
	_ppFixedDividend = &static_cast<boost::shared_ptr<QuantLib::FixedDividend>>(childNative.currentLink());
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFixedDividend));
    _FixedDividendOwner = owner;
}
Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (QuantLib::Handle<QuantLib::FixedDividend> childNative)  : CDividend(CFixedDividend::typeid)
{
	_phFixedDividend = &childNative;
	_ppFixedDividend = &static_cast<boost::shared_ptr<QuantLib::FixedDividend>>(childNative.currentLink());
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFixedDividend));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CFixedDividend::CFixedDividend (QuantLib::FixedDividend childNative)  : CDividend(CFixedDividend::typeid)
{
#ifdef HANDLE
	_phFixedDividend = NULL;
#endif
	_ppFixedDividend = new boost::shared_ptr<QuantLib::FixedDividend> (new QuantLib::FixedDividend (childNative));
    _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (boost::dynamic_pointer_cast<QuantLib::Dividend> (*_ppFixedDividend));
}
#endif

Cephei::QL::Cashflows::CFixedDividend::~CFixedDividend ()
{
    if (_ppFixedDividend != NULL)
    {
	    delete _ppFixedDividend;
        _ppFixedDividend = NULL;
    }
}
Cephei::QL::Cashflows::CFixedDividend::!CFixedDividend ()
{
    if (_ppFixedDividend != NULL)
    {
	    delete _ppFixedDividend;
    }
}
QuantLib::FixedDividend& Cephei::QL::Cashflows::CFixedDividend::GetReference ()
{
    if (_ppFixedDividend == NULL) throw REFNEW NativeNullException ();
	return **_ppFixedDividend;
}
boost::shared_ptr<QuantLib::FixedDividend>& Cephei::QL::Cashflows::CFixedDividend::GetShared ()
{
    if (_ppFixedDividend == NULL) throw REFNEW NativeNullException ();
	return *_ppFixedDividend;
}
QuantLib::FixedDividend* Cephei::QL::Cashflows::CFixedDividend::GetPointer ()
{
    if (_ppFixedDividend == NULL) throw REFNEW NativeNullException ();
	return &**_ppFixedDividend;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FixedDividend>& Cephei::QL::Cashflows::CFixedDividend::GetHandle ()
{
	if (_phFixedDividend == NULL)
	{
		_phFixedDividend = new Handle<QuantLib::FixedDividend> (*_ppFixedDividend);
	}
	return *_phFixedDividend;
}
#endif
bool Cephei::QL::Cashflows::CFixedDividend::HasNative () 
{
	return (_ppFixedDividend != NULL);
}

Double Cephei::QL::Cashflows::CFixedDividend::Amount (Double prm1)
{
    try
    {
        QuantLib::Real _prm1 = (QuantLib::Real)ValueHelper::Convert (prm1); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedDividend)->amount ( _prm1 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CFixedDividend::Amount ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFixedDividend)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IFixedDividend^ Cephei::QL::Cashflows::CFixedDividend_Factory::Create (Double amount, DateTime date)
{
    return REFNEW CFixedDividend ( amount,  date);
}
